﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using WorkflowProcesses = Scenario.WorkflowEngine.DataModel.WorkflowProcesses;
using WorkflowRelevantData = Scenario.WorkflowEngine.DataModel.WorkflowRelevantData;

using Scenario.WorkflowEngine;
using Scenario.Tools.UI.Controls.ListControls.ListItem;

namespace Scenario.Tools.UI.Controls.Workflow
{
    public partial class StartProcess : Form
    {
        private WorkflowProcesses.XMLWorkflow xmlWorkflow = null;

        public StartProcess()
        {
            InitializeComponent();

            this.LoadProcessTemplate();
        }

        private void LoadProcessTemplate()
        {
            WorkflowProcesses.XMLWorkflows xmlWorkflows = Engine.ProcessHelper.GetWorkflowsTemplate();
            WorkflowProcesses.XMLWorkflow xmlWorkflow = null;
            for (int i = 0; i < xmlWorkflows.Count; i++)
            {
                xmlWorkflow = (WorkflowProcesses.XMLWorkflow)xmlWorkflows[i];
                this.ddlProcessTemplate.Items.Add(new Scenario.Tools.UI.Controls.ListControls.ListItem.WorkflowTemplateComboItem(xmlWorkflow));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.lblProcessID.Text = string.Empty;
            this.btnStart.Enabled = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Engine.ProcessHelper.StartWorkflowInstance(this.lblProcessID.Text, null, true);
            MessageBox.Show("Process Started correctly. ID[" + this.lblProcessID.Text + "]");
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.ddlProcessTemplate.SelectedIndex < 0)
            {
                MessageBox.Show("A Process selection is mandatory");
                return;
            }

            this.xmlWorkflow = Engine.ProcessHelper.CreateWorkflowInstance(this.ddlProcessTemplate.Text);
            this.lblProcessID.Text = this.xmlWorkflow.ProcessId;

            this.btnStart.Enabled = true;
        }

    }
}
